package main

import (
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"net/http"
)

func main() {
	//server := web.RegisterRoute()
	InitViper()
	InitLogger()
	server := InitWebServers()

	//handler := server.Handler()
	//httpServer := http.Server{
	//	Addr:    ":9090",
	//	Handler: handler,
	//}
	//httpServer.ListenAndServe()
	//httpServer.Shutdown(context.Background())

	InitPrometheus()
	err := server.Run(":8080")
	if err != nil {
		panic(err)
	}
}

func InitPrometheus() {
	go func() {
		http.Handle("/metrics", promhttp.Handler())
		err := http.ListenAndServe(":9090", nil)
		if err != nil {
			panic(err)
		}
	}()
}

func InitLogger() {
	logger, err := zap.NewDevelopment()
	if err != nil {
		panic(err)
	}
	zap.ReplaceGlobals(logger)
	zap.L().Info("hello")
}

func InitViper() {
	cfile := pflag.String("config", "config/dev.yaml", "config file path")
	pflag.Parse()
	viper.SetConfigFile(*cfile)
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}

}
